package firstping.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import firstping.data.QnAPersistenceManager;
import firstping.model.Customer;

@SuppressWarnings("serial")
public class UpdateServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		HttpSession session = req.getSession();
		String customer = (String) session.getAttribute("customer");
		
		if(customer != null)
		{
			req.setAttribute("title", "Update account");
			req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site");
			req.setAttribute("description", "Update account.We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
			try {
				req.getRequestDispatcher("/update.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			resp.sendRedirect("/register");
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");
		HttpSession session = req.getSession();
		String customer = (String) session.getAttribute("customer");
		ResourceBundle resource = ResourceBundle.getBundle("config");
		if(customer != null)
		{
			String price = req.getParameter("price");
			int pay_price = Integer.parseInt(resource.getString("6_month"));
			if(price != null && price.equals("1"))
			{
				pay_price = Integer.parseInt(resource.getString("6_month"));
			}
			if(price != null && price.equals("2"))
			{
				pay_price = Integer.parseInt(resource.getString("12_month"));
			}
			if(price != null && price.equals("3"))
			{
				pay_price = Integer.parseInt(resource.getString("18_month"));
			}
			if(price != null && price.equals("4"))
			{
				pay_price = Integer.parseInt(resource.getString("24_month"));
			}
			
			Date update = new Date();
			
			PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
			Query query3 = psm.newQuery(Customer.class);
			query3.setFilter("email=='"+customer+"'");
			@SuppressWarnings("unchecked")
			List<Customer> user = (List<Customer>) query3.execute();
			if(user.size()>0)
			{
				user.get(0).setMoney(String.valueOf(pay_price));
				user.get(0).setInvoice(customer+"-"+update.getTime());
				psm=JDOHelper.getPersistenceManager(user.get(0));
				psm.currentTransaction().begin();
				psm.makePersistent(user.get(0));
				psm.currentTransaction().commit();
			}

			resp.getWriter().println("<html>\n");
			resp.getWriter().println("<head><title>Processing Payment...</title></head>\n");
			resp.getWriter().println("<body onLoad=\"document.forms['paypal_form'].submit();\">\n");
			resp.getWriter().println("<center><h2>Please wait, your order is being processed and you");
			resp.getWriter().println(" will be redirected to the paypal website.</h2></center>\n");
			resp.getWriter().println("<form method=\"post\" name=\"paypal_form\" ");
			resp.getWriter().println("action=\"https://www.paypal.com/cgi-bin/webscr\">\n");
			
			resp.getWriter().println("<input type=\"hidden\" name=\"business\" value=\"salem.chinsu@gmail.com\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"return\" value=\"http://www.firstping.com/success\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"cancel_return\" value=\"http://www.firstping.com/update\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"notify_url\" value=\"http://rlink.org/paypal_firstping.php?action=ipn\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"item_name\" value=\""+customer+"\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"invoice\" value=\""+customer+"-"+update.getTime()+"\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"cmd\" value=\"_xclick\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"amount\" value=\""+pay_price+"\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"payer_email\" value=\""+customer+"\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"quantity\" value=\"1\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"currency_code\" value=\"USD\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"no_shipping\" value=\"1\"/>\n");
			resp.getWriter().println("<input type=\"hidden\" name=\"cpp_header_image\" value=\"http://www.firstping.com/images/logos.png\"/>\n");
		      
		    resp.getWriter().println("<center><br/><br/>If you are not automatically redirected to ");
		    resp.getWriter().println("paypal within 5 seconds...<br/><br/>\n");
		    resp.getWriter().println("<input type=\"submit\" value=\"Click Here\"></center>\n");
		      
		    resp.getWriter().println("</form>\n");
		    resp.getWriter().println("</body></html>\n");
		} else {
			resp.sendRedirect("/");
		}
	}
}
